import dayjs from "dayjs";
import { message } from "ant-design-vue";

// 下载文件
export function download(filename, name) {
  if (!filename) {
    message.error("暂无文件!");
    return;
  }

  if (filename.indexOf("txt") > -1) {
    const a = document.createElement("a");
    a.href = "/download/" + filename;
    //路径中'/'为根目录，即index.html所在的目录
    a.download = name;
    a.click();
    return;
  }
  window.open("/download/" + filename);
}

// 时间处理
export function setTime(date, type) {
  switch (type) {
    case 1:
      return dayjs(date).format("YYYY-MM-DD");
      break;
    case 2:
      return dayjs(date).format("YYYY-MM-DD HH:mm:ss");
      break;
  }
}

/**
 * 遍历children形式数据
 * @param data 需要遍历的数组
 * @param callback 回调
 * @param childKey children字段名
 */
export function eachTreeData(data, callback, childKey = "children") {
  if (!data || !data.length) {
    return;
  }
  data.forEach((d) => {
    if (callback(d) !== false && d[childKey]) {
      eachTreeData(d[childKey], callback, childKey);
    }
  });
}
